import json
from enum import Enum, unique
from conf.settings import logger


# 装饰器
def log_func(func):
    """
    打印函数入参和返回值的装饰器
    :param func: 函数对象
    :return:
    """

    def wrapper(*args, **kwargs):
        # 把参数合成一个签名字符串
        args_repr = [repr(a) for a in args]
        kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
        signature = ", ".join(args_repr + kwargs_repr)
        logger.info(f"===>调用 {func.__name__}({signature})")

        # 记录函数返回值
        value = func(*args, **kwargs)
        logger.info(f"<==={func.__name__!r} 返回了 {value!r}")

        return value

    return wrapper


@unique
class Status(Enum):
    ERROR = -1
    FAILURE = 0
    SUCCESS = 1


class Response:

    def __init__(self, status: Status = Status.SUCCESS, message: str = '', data=None):
        self.status = status.value
        self.message = message
        self.data = data or {}

    def __str__(self):
        return json.dumps({key: value for key, value in self.__dict__.items()}, ensure_ascii=False)


def respond(response: Response):
    return json.loads(str(response))

